﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Linq;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using TestTracking.Common;
using TestTracking.Helper;
using TestTracking.Interface;

namespace TestTracking.ViewModel
{
    public class SettingViewModel:ViewModelBase
    {
        IXmlHelp xmlHelp = null;
        public SettingViewModel()
        {
            xmlHelp = new XmlHelp("");
        }
        #region 属性
        private string server;
        /// <summary>
        /// 服务器地址
        /// </summary>
        public string Server
        {
            get { return server; }
            set { server = value; this.RaisePropertyChanged(() => Server); }
        }
        private string userId;
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserId
        {
            get { return userId; }
            set { userId = value; this.RaisePropertyChanged(() => UserId); }
        }
        private string password;
        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get { return password; }
            set { password = value; this.RaisePropertyChanged(() => Password); }
        }
        private string tableName;
        /// <summary>
        /// 表名
        /// </summary>
        public string TableName
        {
            get { return tableName; }
            set { tableName = value; this.RaisePropertyChanged(() => TableName); }
        }
        private string station;
        /// <summary>
        /// 站位
        /// </summary>
        public string Station
        {
            get { return station; }
            set { station = value; this.RaisePropertyChanged(() => Station); }
        }
        private ObservableCollection<string> stations;
        /// <summary>
        ///站位集合
        /// </summary>
        public ObservableCollection<string> Stations
        {
            get { return stations; }
            set { stations = value; this.RaisePropertyChanged(() => Stations); }
        }
        private string result;
        /// <summary>
        /// 站位
        /// </summary>
        public string Result
        {
            get { return result; }
            set { result = value; this.RaisePropertyChanged(() => Result); }
        }
        private ObservableCollection<string> results;
        /// <summary>
        ///站位集合
        /// </summary>
        public ObservableCollection<string> Results
        {
            get { return results; }
            set { results = value; this.RaisePropertyChanged(() => Results); }
        }
        private string sequence;
        /// <summary>
        /// 站位
        /// </summary>
        public string Sequence
        {
            get { return sequence; }
            set { sequence = value; this.RaisePropertyChanged(() => Sequence); }
        }
        private ObservableCollection<string> sequences;
        /// <summary>
        ///站位集合
        /// </summary>
        public ObservableCollection<string> Sequences
        {
            get { return sequences; }
            set { sequences = value; this.RaisePropertyChanged(() => Sequences); }
        }
        #endregion

        #region 命令
        private RelayCommand loadedPageCmd;

        public RelayCommand LoadedPageCmd
        {
            get {
                loadedPageCmd = loadedPageCmd ?? new RelayCommand(LoadedPage);
                return loadedPageCmd; }
            set { loadedPageCmd = value; }
        }

        private RelayCommand saveCmd;

        public RelayCommand SaveCmd
        {
            get {
                saveCmd = saveCmd ?? new RelayCommand(SaveAsync);
                return saveCmd; }
            set { saveCmd = value; }
        }
        private RelayCommand cancelCmd;

        public RelayCommand CancelCmd
        {
            get {
                cancelCmd = cancelCmd ?? new RelayCommand(Cancle);
                return cancelCmd; }
            set { cancelCmd = value; }
        }
        private RelayCommand addStationCmd;

        public RelayCommand AddStationCmd
        {
            get {
                addStationCmd = addStationCmd ?? new RelayCommand(AddNewStation);
                return addStationCmd; }
            set { addStationCmd = value; }
        }
        private RelayCommand addResultCmd;

        public RelayCommand AddResultCmd
        {
            get {
                addResultCmd = addResultCmd ?? new RelayCommand(AddNewResult);
                return addResultCmd; }
            set { addResultCmd = value; }
        }
        private RelayCommand addSequenceCmd;

        public RelayCommand AddSequenceCmd
        {
            get {
                addSequenceCmd = addSequenceCmd ?? new RelayCommand(AddNewSequence);
                return addSequenceCmd; }
            set { addSequenceCmd = value; }
        }
        private RelayCommand<string> lstStationMouseDoubleClick;

        public RelayCommand<string> LstStationMouseDoubleClick
        {
            get {
                lstStationMouseDoubleClick = lstStationMouseDoubleClick ?? new RelayCommand<string>(StationMouseDoubleClickHandler);
                return lstStationMouseDoubleClick; }
            set { lstStationMouseDoubleClick = value; }
        }
        private RelayCommand<string> lstResultsMouseDoubleClick;

        public RelayCommand<string> LstResultsMouseDoubleClick
        {
            get {
                lstResultsMouseDoubleClick = lstResultsMouseDoubleClick ?? new RelayCommand<string>(ResultMouseDoubleClickHandler);
                return lstResultsMouseDoubleClick; }
            set { lstResultsMouseDoubleClick = value; }
        }
        private RelayCommand<int> lstSequenceMouseDoubleClick;

        public RelayCommand<int> LstSequenceMouseDoubleClick
        {
            get {
                lstSequenceMouseDoubleClick = lstSequenceMouseDoubleClick ?? new RelayCommand<int>(SequenceMouseDoubleClickHandler);
                return lstSequenceMouseDoubleClick; }
            set { lstSequenceMouseDoubleClick = value; }
        }

        #endregion

        #region 方法
        //页面加载
        private void LoadedPage()
        {
            AddStationToComboBox();
            AddResultToComboBox();
            AddSequenceToComboBox();
            FetchSqlParameters();
        }

        private void StationMouseDoubleClickHandler(string obj)
        {
            if (obj!=null)
            {
                Stations.Remove(obj);
            }
        }

        private void ResultMouseDoubleClickHandler(string obj)
        {
            if (obj!=null)
            {
                Results.Remove(obj);
            }
        }

        private void SequenceMouseDoubleClickHandler(int obj)
        {
            Sequence.Remove(obj);
        }

        void AddStationToComboBox()
        {
            Stations = new ObservableCollection<string>();
            string strStations = xmlHelp.GetNoteValue<string>("UIParameterInfo/Stations");
            string[] arrayStations = strStations.Split('|');
            foreach (var s in arrayStations)
            {
                Stations.Add(s);
            }
        }
        void AddResultToComboBox()
        {
            Results = new ObservableCollection<string>();
            string strResults = xmlHelp.GetNoteValue<string>("UIParameterInfo/Results");
            string[] arrayResults = strResults.Split('|');
            foreach (var r in arrayResults)
            {
                Results.Add(r);
            }
        }
        void AddSequenceToComboBox()
        {
            Sequences = new ObservableCollection<string>();
            string strSequences = xmlHelp.GetNoteValue<string>("UIParameterInfo/Sequences");
            string[] arraySequences = strSequences.Split('|');
            foreach (var s in arraySequences)
            {
                Sequences.Add(s);
            }
        }
        void FetchSqlParameters()
        {
            XElement sqlElement = xmlHelp.GetElement("SqlInfo");
            Server = sqlElement.Element("Server").Value;
            UserId = sqlElement.Element("UserId").Value;
            Password = sqlElement.Element("Password").Value;
            TableName = sqlElement.Element("TableName").Value;
        }
        //保存配置
        private void SaveAsync()
        {
            if (Server=="")
            {
                MessageBox.Show("服务器地址不能为空");
                return;
            }

            if (UserId == "")
            {
                MessageBox.Show("用户名不能为空");
                return;
            }

            if (Password == "")
            {
                MessageBox.Show("密码不能为空");
                return;
            }

            if (TableName == "")
            {
                MessageBox.Show("表名不能为空");
                return;
            }

            if (Stations == null||Stations.Count==0)
            {
                MessageBox.Show("站位信息不能为空");
                return;
            }
            if (Results == null || Results.Count == 0)
            {
                MessageBox.Show("结果不能为空");
                return;
            }
            if (Sequences == null || Sequences.Count == 0)
            {
                MessageBox.Show("序列不能为空");
                return;
            }
            XElement sqlElement = xmlHelp.GetElement("SqlInfo");
            sqlElement.Element("Server").Value=Server;
            sqlElement.Element("UserId").Value=UserId;
            sqlElement.Element("Password").Value=Password;
            sqlElement.Element("TableName").Value=TableName;

            XElement UIParameterInfo = xmlHelp.GetElement("UIParameterInfo");
            UIParameterInfo.Element("Stations").Value = string.Join("|", Stations.ToArray());
            UIParameterInfo.Element("Results").Value = string.Join("|",Results.ToArray());
            UIParameterInfo.Element("Sequences").Value = string.Join("|",Results.ToArray());
            xmlHelp.Save();
        }
        //取消
        private void Cancle()
        {
            ViewManager.Close("Setting");
        }
        //添加新的站位
        private void AddNewStation()
        {
            if ((Station??"")!="")
            {
                if (!Stations.Contains(Station))
                {
                    Stations.Add(Station);
                    Station = "";
                }
            }
        }

        private void AddNewResult()
        {
            if ((Result??"") != "")
            {
                if (!Results.Contains(Result))
                {
                    Results.Add(Result);
                    Result = "";
                }
            }
        }

        private void AddNewSequence()
        {
            if ((Sequence??"") != "")
            {
                if (!Sequences.Contains(Sequence))
                {
                    Sequences.Add(Sequence);
                    Sequence = "";
                }
            }
        }
        #endregion
    }
}
